from OCC.Extend.DataExchange import read_iges_file
from OCC.Core.BRepAdaptor import BRepAdaptor_Curve
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_EDGE
from OCC.Core.TopoDS import topods
from OCC.Core.GeomLProp import GeomLProp_CurveTool
from OCC.Core.gp import gp_Pnt

# 加载零件模型文件
part = read_iges_file("assets/recognize/0109.IGS")  # 替换为你的零件模型文件路径

# 提取拉伸体边界线
edge_explorer = TopExp_Explorer(part, TopAbs_EDGE)
edges = []

while edge_explorer.More():
    edge = topods.Edge(edge_explorer.Current())
    edges.append(edge)
    edge_explorer.Next()

# 判定方管壁厚
thicknesses = []

for edge in edges:
    curve = BRepAdaptor_Curve(edge)
    p1 = curve.Value(curve.FirstParameter())
    p2 = curve.Value(curve.LastParameter())
    midpoint = gp_Pnt((p1.X() + p2.X()) / 2.0, (p1.Y() + p2.Y()) / 2.0, (p1.Z() + p2.Z()) / 2.0)
    normal = curve.Line().Direction().Normalized()
    offset_point = gp_Pnt(midpoint.X() + normal.X(), midpoint.Y() + normal.Y(), midpoint.Z() + normal.Z())
    projected_point = GeomLProp_CurveTool.Value(curve.Curve(), curve.FirstParameter(), offset_point.Z())
    thickness = offset_point.Distance(projected_point)
    thicknesses.append(thickness)

# 壁厚为最小值
if thicknesses:
    min_thickness = min(thicknesses)
    print("方管壁厚: ", min_thickness)
else:
    print("未找到方管拉伸体边界线")